﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using System.Xml.Serialization;
using Newtonsoft.Json;

namespace Tools.Framework
{
    [Serializable]
    [XmlRootAttribute("Root", Namespace = "", IsNullable = false)]
    public class MessageResult
    {
        public MessageResult()
        {
            this.msg = "操作成功";
            this.success = true;
            this.code = "success";
        }
        public bool success { set; get; }
        public string code { set; get; }
        public string msg { set; get; }

        [XmlIgnoreAttribute()]
        public object data { set; get; }


        public static MessageResult ErrorResult(bool success = false, string code = "error", string msg = "操作失败！", object data = null)
        {
            var result = new MessageResult
            {
                success = success,
                code = code,
                msg = msg,
                data = data
            };
            return result;
        }

        public static MessageResult SuccessResult(bool success = true, string code = "success", string msg = "操作成功！", object data = null)
        {
            var result = new MessageResult
            {
                success = success,
                code = code,
                msg = msg,
                data = data
            };
            return result;
        }

    }

    public static class MessageResultExtend
    {
        /// <summary>
        /// 调用原生json序列化
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static JsonResult ToJsonResult(this MessageResult result)
        {
            var jsonresult = new JsonResult()
            {
                ContentEncoding=Encoding.UTF8,
                ContentType = "text/json",
                Data = result,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
            return jsonresult;
        }
        /// <summary>
        /// 调用原生json序列化
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public static ContentResult ToContentResult(this MessageResult result)
        {
            //result.ToJsonString()
            Newtonsoft.Json.Converters.IsoDateTimeConverter convert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            var settings = new JsonSerializerSettings
            {
                //这句是解决问题的关键,也就是json.net官方给出的解决配置选项.                 
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                Converters = new List<JsonConverter>() { convert }
            };
            var str = Newtonsoft.Json.JsonConvert.SerializeObject(result, settings);
            var contentresult = new ContentResult()
            {
                ContentEncoding = Encoding.UTF8,
                ContentType = "text/json",
                Content = str
            };
            return contentresult;
        }
    }
}
